package cnn.test.service;

import cnn.sys.RestSession;
import cnn.user.entity.DeptDTO;
import cnn.user.entity.UserDTO;
import cnn.user.entity.UserQTO;
import cnn.user.service.DeptService;
import cnn.user.service.UserService;
import cnn.topfox.common.Response;
import cnn.topfox.data.DbState;
import cnn.topfox.service.MultiService;
import cnn.topfox.sql.Condition;
import cnn.topfox.sql.EntitySelect;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 常用增删改查 单元测试用的源码
 */
@Service
@Slf4j
public class UnitTestService extends MultiService<RestSession> {
    @Autowired
    UserService userService;
    @Autowired
    DeptService deptService;

    public Object query(){

        UserDTO user = userService.getObject("0001");
        UserDTO user2 = userService.getObject("0002", false);
        List<UserDTO> list = userService.listObjects("0003", "0004");

        Set setIds = new HashSet<>();
        setIds.add(111);
        setIds.add(222);
        userService.listObjects(setIds);
        userService.listObjects(setIds, false);

        UserQTO userQTO = new UserQTO();
        userQTO.setIdOrEq("0005,0006") //需要Mapper.xml支持
                .setOrderBy("sex")
                .setPageSize(10);
        userService.list(userQTO);

        list= userService.listObjects(
                EntitySelect.create().select("id, name, sex")
                        .where()
                        .like("name","张三")
                        .eq("id","999")
                        .and()
                        .setQTO(userQTO)
                        .endWhere()
        );

        List<DeptDTO> list2 =deptService.listObjects(
                where().eq("deptName","IT部门")
        );
        list2 =deptService.listObjects(
                Condition.create()
                        .eq("deptName","IT部门")
        );

        List<JSONObject> listMaps =deptService.selectMaps(
                Condition.create()
                        .eq("deptName","IT部门")
        );

        /**
         * 自定分页查询查询
         */
        Response<List<UserDTO>> response=userService.listPage(
                select("sex,count(1)")
                        .where()
                        .eq("deptId",10)
                        .endWhere()
                        .groupBy("sex")
                        .orderBy("sex")
                        .setPage(3,100)
        );

        response=userService.listPage(
                select("sex,count(1)")
                        .where()
                        .eq("deptId",10)
                        .endWhere()
                        .groupBy("sex")
                        .orderBy("sex")
                        .setPageSize(3)
        );


//        List<ProgramDTO> list = programService.updateBatch(new ProgramDTO().setRemark("333"),
//                where().eq("moduleName","个人设置")
//        );


        list=userService.listObjects(
                select("sex, count('*')") //通过调用SimpleService.select() 获得或创建一个新的 EntitySelect 对象,并返回它
                        .where()         //创建一个新 Condition对象,并返回它
                        .eq("sex","男")    //条件匹配器自定义条件 返回对象 Condition
                        .endWhere()      //条件结束           返回对象 EntitySelect
                        .orderBy("name") //设置排序的字段      返回对象 EntitySelect
                        .groupBy("name") //设置分组的字段      返回对象 EntitySelect
                        .setPage(1,5)   //设置分页 返回对象 EntitySelect

        );

        List<UserDTO> list11 = userService.listObjects(select()   //select 无参数
                .where().eq("sex","男")
        );

        List<UserDTO> list22 = userService.listObjects(select("id,name,sex") //select 一个参数
                .where().eq("sex","男")
        );

        List<UserDTO> list33 = userService.listObjects(select("length(name)", true)//select 两个参数
                .where().eq("sex","男")
        );

        userService.selectCount(where().all());

        //求指定字段的最大值
        userService.selectMax("loginCount", where().all());


        return null;
    }


    // select() 方法运用例子
    public Object query1(){
        List<UserDTO> list = userService.listObjects(
                select("id, name, sex")
                        .where()
                        .like("name","张三")
                        .eq("id","999")
                        //.endWhere()
        );
        return list;
    }

    // where() 方法运用例子
    public Object query2(){
        List<DeptDTO> list =deptService.listObjects(
                where().eq("deptName","IT部门")
        );
        return list;
    }

    public void insert(){
        //Id为数据库自增, 新增可以获得Id
        UserDTO dto = new UserDTO();
        dto.setName("张三");
        dto.setSex("男");
        userService.insert(dto);
        log.debug("新增用户的Id 是 {}", dto.getId());
    }

    public void update(){
        UserDTO user1 = new UserDTO();
        user1.setAge(99);
        user1.setId(1);
        user1.setName("Luoping");

        //将指定的字段更新为null, 允许有空格
        user1.addNullFields("sex,lastDate","loginCount");
        user1.addNullFields("isAdmin");

//        //这样写也支持
//        user1.addNullFields("sex","lastDate");
//        //这样写也支持
//        user1.addNullFields("sex, lastDate","deptId");

        userService.update(user1);//只更新有值的字段
    }

    public void update1(){
        UserDTO user1 = new UserDTO();
        user1.setAge(99);
        user1.setId(1);
        user1.setName("Luoping");

        userService.update(user1);//只更新有值的字段
    }

    public void updateList(){
        UserDTO user1 = new UserDTO();
        user1.setAge(99);
        user1.setId(1);
        user1.setName("张三");
        user1.addNullFields("sex, lastDate");

        UserDTO user2 = new UserDTO();
        user2.setAge(88);
        user2.setId(2);
        user2.setName("李四");
        user2.addNullFields("mobile, isAdmin");

        List list = new ArrayList();
        list.add(user1);
        list.add(user2);
        userService.updateList(list);//只更新有值的字段


//        //TopFox将 自动调用 service.update 方法
//        user1.dataState(DbState.UPDATE);
//
//        //TopFox将 自动调用 service.insert 方法
//        user2.dataState(DbState.INSERT);
//        UserDTO user3 = new UserDTO();
//        user3.setId(26);
//
//        //TopFox将 自动调用 service.delete 方法
//        user3.dataState(DbState.DELETE);
//
//
//        list.add(user3);
//        userService.save(list);
    }

    public void saveTest(){
        UserDTO user1 = new UserDTO();
        user1.setAge(18);
        user1.setId(1);
        user1.setName("张三");
        user1.addNullFields("sex, lastDate");

        UserDTO user2 = new UserDTO();
        user2.setAge(19);
        user2.setId(2);
        user2.setName("李四");
        user2.addNullFields("mobile, isAdmin");

        UserDTO user3 = new UserDTO();
        user3.setId(3);


        List list = new ArrayList();
        //TopFox将 自动调用 service.insert 方法
        user1.dataState(DbState.INSERT);
        //user1.setState(DbState.INSERT);

        //TopFox将 自动调用 service.update 方法
        user2.dataState(DbState.UPDATE);
        //user2.setState(DbState.UPDATE);

        //TopFox将 自动调用 service.delete 方法
        user3.dataState(DbState.DELETE);
        //user3.setState(DbState.DELETE);

        list.add(user1);
        list.add(user2);
        list.add(user3);
        userService.save(list);
    }

    public void updateBatch(){
        UserDTO dto = new UserDTO();
        dto.setAge(99);
        dto.setDeptId(11);
        dto.addNullFields("mobile, isAdmin");

        userService.updateBatch(dto, where().eq("sex","男"));
    }

    public void findAndUpdate(){
        UserDTO user1 = userService.getObject(1);
        //增加这行代码就能实现 根据变化值更新, 必须要放在set新值之前
        user1.addOriginFromCurrent();

        user1.setAge(null);
        user1.setName("王五");
        userService.update(user1);
    }

    /**
     * 递增更新
     */
    public void addUpdate(){
        UserDTO user1 = new UserDTO().setId(1).setLoginCountAdd(10);
        userService.update(user1);
    }

}